package com.white.commonlib.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.util.AttributeSet;

import com.white.commonlib.R;


/**
 * Created by yutf on 2017/5/27 0027.
 */

public class ShadowView extends AbsView
{
    private Paint mPaint;
    private int shadowColor;
    private int derection;

    public ShadowView(Context context)
    {
        super(context);
        init();
    }

    public ShadowView(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.ShadowView);
        shadowColor = ta.getColor(R.styleable.ShadowView_sv_color, 0x10008aff);
        derection = ta.getInteger(R.styleable.ShadowView_sv_derection, 0);
        init();
    }

    private void init()
    {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(shadowColor);
    }

    @Override
    public int getDefaultWrapWidth()
    {
        return 500;
    }

    @Override
    public int getDefaultWrapHeight()
    {
        return 20;
    }

    int height,width;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        super.onSizeChanged(w, h, oldw, oldh);
        height = getMeasuredHeight();
        width = getMeasuredWidth();
        LinearGradient lg = null;
        if(derection == 0)
        {
            lg = new LinearGradient(width / 2, 0, width/2, height, shadowColor,0x00000000, Shader.TileMode.CLAMP);
        }else
        {
            lg = new LinearGradient(width / 2, height, width/2, 0, shadowColor,0x00000000, Shader.TileMode.CLAMP);
        }
        mPaint.setShader(lg);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        canvas.drawColor(Color.TRANSPARENT);
        canvas.drawRect(0,0, width, height, mPaint);
    }

    @Override
    public void onGlobalLayout()
    {

    }
}
